from pypinyin import pinyin,Style

def chinese_sentence2pinyin_sequence(label,_):
    # 第二个参数是(对照表内容，但这里不需要了)
    return [py[0] for py in pinyin(label,style=Style.TONE3,neutral_tone_with_five=True)]

def load_pinyin_dict(filename):
    pinyin_dict=dict()
    with open(filename,'r',encoding='utf8') as f:
        lines=f.read().split('\n')
    c=0
    for line in lines:
        if len(line)==0:
            continue
        tokens=line.split('\t')
        pinyin_dict[tokens[0]]=c
        c+=1
    return pinyin_dict

def enc_sequence(sequence,penc_dict):
    return [penc_dict[s] for s in sequence]

def load_id2pinyin_dict(filename):
    id2pinyin=dict()
    with open(filename,'r',encoding='utf8') as f:
        lines=f.read().split('\n')
    c=0
    for line in lines:
        if len(line)==0:
            continue
        tokens=line.split('\t')
        id2pinyin[c]=tokens[0]
        c+=1
    return id2pinyin

def load_pinyin2chinese_word(filename):
    pinyin2chinese_word=dict()
    with open(filename,'r',encoding='utf8') as f:
        lines=f.read().split('\n')
    for line in lines:
        if len(line)==0:
            continue
        tokens=line.split('\t')
        pinyin2chinese_word[tokens[0]]=tokens[1]
    return pinyin2chinese_word

def dec_sequence(ids,id2pinyin):
    return [id2pinyin[id] for id in ids]

def pinyin2chinese_word(pinyin,pinyin2cw):
    return pinyin2cw[pinyin]